package com.example.microinvestweb.controller.user;
import com.example.microinvestcommon.pojo.Result;
import com.example.microinvestcommon.util.AliOssUtil;
import com.example.microinvestuser.service.user.UserUploadService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.UUID;

@RestController
public class FileUploadController {
    private UserUploadService userUploadService;

    @Autowired
    public FileUploadController (UserUploadService userUploadService){
        this.userUploadService=userUploadService;
    }


    @SneakyThrows
    @PostMapping("/account/upload")
    public Result<String> upload(MultipartFile file) throws IOException {

        String originalFileName=file.getOriginalFilename();
        String filename= UUID.randomUUID().toString()+originalFileName.substring(originalFileName.lastIndexOf("."));
//        file.transferTo(new File(""+filename));
        String url = AliOssUtil.uploadFile(filename,file.getInputStream());

        Result<String> result=userUploadService.uploadUserProfile(url);
        return result;
    }
}
